#include <iostream>
#include <client/dbclient.h>
using namespace std;
using namespace mongo;
#include <QApplication>
#include <QLabel>
#include <QSplashScreen>
#include <QTextCodec>
#include "mainwindow.h"
#include "preferences.h"
#ifndef WIN32
void initCrashHandler();
static void defaultCrashHandler(int sig);
#endif
#include "utils.h"
int main(int argc,char* argv[])
{

    try{
    QApplication app(argc,argv);
    app.setWindowIcon(QPixmap(":/images/mongodb_logo.png"));
#ifndef WIN32
    initCrashHandler();
#endif
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
   // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));

    int style = Preferences::instance()->GUIstyle();
    if (style != 0)
    {
            bool styleSuccess = false;
            QStringList sl = QStyleFactory::keys();
            sl.sort();
            if (sl.count() > (style-1))
            {
                    QStyle * s = QStyleFactory::create(sl.at(style-1));
                    if (s)
                    {
                            QApplication::setStyle(s);
                            styleSuccess = true;
                    }
            }
            if (!styleSuccess)
            {
                    QTextStream cout(stdout, QIODevice::WriteOnly);
                    cout << "Cannot setup GUI style. Default is used.";
            }
    }

    QSplashScreen* splash=new QSplashScreen;

    splash->setPixmap(QPixmap(":/images/mongodb_logo.png"));
    splash->show();
    Qt::Alignment topRight=Qt::AlignRight|Qt::AlignTop;
    splash->showMessage(QObject::tr("Setting up the main window..."),topRight,Qt::white);
    Utils::sleep(1);
    splash->showMessage(QObject::tr("Loading modules..."),topRight,Qt::white);
    Utils::sleep(1);
    MainWindow::getInstance()->show();
    splash->finish(MainWindow::getInstance());
    delete splash;
    return app.exec();
    }catch(std::exception& e)
    {
        qDebug()<<"final exception:"<<e.what()<<"\n";
    }
}
#ifndef WIN32
void initCrashHandler()
{
        typedef void (*HandlerType)(int);
        HandlerType handler	= 0;
        handler = defaultCrashHandler;
        if (!handler)
                handler = SIG_DFL;
        sigset_t mask;
        sigemptyset(&mask);
#ifdef SIGSEGV
        signal (SIGSEGV, handler);
        sigaddset(&mask, SIGSEGV);
#endif
#ifdef SIGFPE
        signal (SIGFPE, handler);
        sigaddset(&mask, SIGFPE);
#endif
#ifdef SIGILL
        signal (SIGILL, handler);
        sigaddset(&mask, SIGILL);
#endif
#ifdef SIGABRT
        signal (SIGABRT, handler);
        sigaddset(&mask, SIGABRT);
#endif
        sigprocmask(SIG_UNBLOCK, &mask, 0);
}


void defaultCrashHandler(int sig)
{
        QTextStream cout(stdout, QIODevice::WriteOnly);
        static int crashRecursionCounter = 0;
        crashRecursionCounter++;
        signal(SIGALRM, SIG_DFL);
        if (crashRecursionCounter < 2)
        {
                crashRecursionCounter++;
                QString sigMsg(QString("\nMongod Client crashes due to Signal #%1\n\n"
                                       "All database opened will be rollbacked and closed.\n\n"
                                       "Collect last steps that forced this\n\n"
                                       "situlation and report it as a bug, please.").arg(sig));
                cout << sigMsg << endl;
                QMessageBox::critical(0, "Mongod Client", sigMsg);
                alarm(300);
        }
        exit(255);
}
#endif
